package threadLock.lock.AQS;

import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 使用reentrant锁 将普通的arrayList变成一个线程安全的list
 */
public class ReentrantLockList {

    private ArrayList<String> array = new ArrayList<>();

    private volatile ReentrantLock lock = new ReentrantLock();

    public void add(String e){
        lock.lock();
        try {
            array.add(e);
        } finally {
            lock.unlock();
        }
    }

    public void remove(String e){
        lock.lock();
        try{
            array.remove(e);
        } finally {
            lock.unlock();
        }
    }

    public String get(int i){
        lock.lock();
        try{
            return array.get(i);
        } finally {
            lock.unlock();
        }
    }
}
